/* http://snipplr.com/view/19838/get-url-parameters/ */
function getUrlVars()
{
  var vars = {};
  var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
      vars[key] = value;
  });
  return vars;
}

function update_total(cardid)
{
  try {
    ajaxRequest = new XMLHttpRequest();

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
	        var ajaxDisplay = document.getElementById('total_'+cardid);
	        ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
      }

    var urlVars = getUrlVars();
    if(urlVars.collectionid)
    {
      var collectionid = parseInt(urlVars.collectionid);
    }
    else if(urlVars.deckid)
    {
      var collectionid = parseInt(urlVars.deckid);
    }
    ajaxRequest.open("GET", "./php_scripts/get_total_amount.php?cardid="+cardid+"&collectionid="+collectionid, true);
    ajaxRequest.send(null);
  } catch (e) {
    alert("error"+e);
  }
}

function increase_amount_in_collection(collectionid, cardid, foil)
{
  try {
    ajaxRequest = new XMLHttpRequest();

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
	        var ajaxDisplay = document.getElementById('amount_collection_'+collectionid+'_'+cardid);
	        ajaxDisplay.innerHTML = ajaxRequest.responseText;
          //set total amount, this however, allowes for negative numbers, a browser refresh fixes this          
          update_total(cardid);
        }
      }

    ajaxRequest.open("GET", "./php_scripts/update_database.php?cardid="+cardid+"&collectionid="+collectionid+"&action=ADD&foil="+foil+"", true);
    ajaxRequest.send(null);
  } catch (e) {
    alert("error");
  }
}


function decrease_amount_in_collection(collectionid, cardid, foil)
{
  try {
    ajaxRequest = new XMLHttpRequest();

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
	        var ajaxDisplay = document.getElementById('amount_collection_'+collectionid+'_'+cardid);
	        ajaxDisplay.innerHTML = ajaxRequest.responseText;
          //set total amount, this however, allowes for negative numbers, a browser refresh fixes this
          update_total(cardid);
        }
      }

    ajaxRequest.open("GET", "./php_scripts/update_database.php?cardid="+cardid+"&collectionid="+collectionid+"&action=DEL&foil="+foil+"", true);
    ajaxRequest.send(null);
  } catch (e) {
    alert("error");
  }
}
